package com.nofeng.wx.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

public class BaseUtils {

    public static Integer getDate  (Integer amount){
        Date date = DateUtils.addDays(new Date(), amount);
        return Integer.valueOf(DateFormatUtils.format(date, FastDateFormat.getInstance("yyyyMMdd").getPattern()));
    }

    public static Integer getDate  (Date date,Integer amount){
        date = DateUtils.addDays(date, amount);
        return Integer.valueOf(DateFormatUtils.format(date, FastDateFormat.getInstance("yyyyMMdd").getPattern()));
    }

    public static Date dataFormatYYYYMMDD(Integer yyyymmdd){
        try {
            Date date= DateUtils.parseDate(yyyymmdd.toString(),new String[] {"yyyyMMdd"});
            return date;
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Integer getYear  (Integer amount){
        Date date = DateUtils.addYears(new Date(), amount);
        return Integer.valueOf(DateFormatUtils.format(date, FastDateFormat.getInstance("yyyy").getPattern()));
    }

    public static JSONObject jsonObject(String jsonStr ){
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            return jsonObject;
        }catch (Exception e){
            return null;
        }

    }

    public static String doubleToPtStr(Double v){
        if(null!=v) {
            return String.format("%.2f%%", v * 100);
        }
        return "0.00%";
    }

    public static BigDecimal stringToBigDecimal(String v){
        if(StringUtils.isEmpty(v)){
            return null;
        }
        try{
            return new BigDecimal(v);
        }catch (Exception e){
            return null;
        }
    }

    public static Integer stringToInteger(String v){
        if(StringUtils.isEmpty(v)){
            return null;
        }
        try{
            return new Integer(v);
        }catch (Exception e){
            return null;
        }
    }

    public static String format(Double v){
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(v);
    }
    public static List<Double> toList(List<BigDecimal> bigDecimalList){
        return bigDecimalList.stream().map(BigDecimal::doubleValue).collect(Collectors.toList());
    }

    @SafeVarargs
    public static <K, V> Map<K, V> mapOf(Object... entries) {
        if (entries.length % 2 != 0) {
            throw new IllegalArgumentException("参数数量必须为偶数");
        }
        Map<K, V> map = new HashMap<>();
        for (int i = 0; i < entries.length; i += 2) {
            K key = (K) entries[i];
            V value = (V) entries[i+1];
            map.put(key, value);
        }
        return Collections.unmodifiableMap(map);
    }
}
